<template>
  <div>
    <pdf v-for="i in pageCount" :key="i" :src="pdfSrc" :page="i" />
  </div>
</template>

<script>

import Pdf from 'vue-pdf'
import CMapReaderFactory from 'vue-pdf/src/CMapReaderFactory'
import { getPdfBase64 } from '@/views/vue-pdf/pdfBase64'
export default {
  name: 'VuePdf',
  components: { Pdf },
  data() {
    return {
      pageCount: 0,
      page: 0,
      currentPage: 0,
      pdfData: getPdfBase64()

    }
  },
  computed: {
    pdfSrc() {
      const pdfBase64String = this.pdfData
      if (!pdfBase64String) {
        return
      }
      return Pdf.createLoadingTask({ url: 'data:application/pdf;base64,' + pdfBase64String, CMapReaderFactory })
    }
  },
  watch: {
    pdfSrc: {
      immediate: true,
      deep: true,
      handler: function(val) {
        if (!val) {
          return
        }
        val.promise.then(pdf => {
          const { numPages } = pdf
          this.pageCount = numPages
        }).catch((e) => {
          console.error('有毒')
        })
      }
    }
  }

}
</script>
